//
//  ViewController.m
//  LXModelExtensionExample
//
//  Created by imera on 16/5/14.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "ViewController.h"
#import "LXStudent.h"
#import "NSObject+LXModelExtension.h"

@interface ViewController ()

@property (nonatomic, strong) UIButton *btn1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    //1.简单字典转模型
//    [self dictToModel];
    
//    //2.简单模型转字典
//    [self modelToDict];
    
//    //3.复杂字典转模型（模型中嵌套模型）
//    [self complexDictToModel];
    
//    //4.复杂模型转字典
//    [self complexModelToDict];

//    //5.字典数组转模型数组
//    [self dictArrayToModelArray];
    
    //6.字典转模型，多级映射
    [self dictToModelWithMap];
}

//1.简单字典转模型
- (void)dictToModel
{
    NSDictionary *dict = @{
        @"english": @(98),
        @"math": @(87),
        @"chinese": @(59)
    };
    LXProgress *progress = [LXProgress lx_setWithDict:dict];
    NSLog(@"简单字典转模型:english:%d---math:%d---chinese:%d", progress.english, progress.math, progress.chinese);
}

//2.简单模型转字典
- (void)modelToDict
{
    LXProgress *progress = [[LXProgress alloc]init];
    progress.math = 99;
    progress.english = 76;
    progress.chinese = 89;
    
    NSDictionary *dict = progress.lx_dict;
    NSLog(@"简单模型转字典:%@", dict);
}

//3.复杂字典转模型（模型中嵌套模型）
- (void)complexDictToModel
{
    NSDictionary *dict = @{
        @"name": @"aaa",
        @"age": @(23),
        @"age11": @(129999),
        @"progress": @{
            @"math": @(99),
            @"chinese": @(78),
            @"english": @(20)
        }
    };
    LXStudent *st = [LXStudent lx_setWithDict:dict];
    NSLog(@"复杂字典转模型（模型中嵌套模型）:name:%@---age:%d", st.name, st.age);
    
    LXProgress *progress = st.progress;
    NSLog(@"复杂字典转模型（模型中嵌套模型）:math:%d---chinese:%d---english:%d", progress.math, progress.chinese, progress.english);
}

//4.复杂模型转字典
- (void)complexModelToDict
{
    LXProgress *progress = [[LXProgress alloc]init];
    progress.math = 88;
    progress.chinese = 98;
    progress.english = 56;
    
    LXStudent *st = [[LXStudent alloc]init];
    st.name = @"张三";
    st.age = 18;
    st.progress = progress;
    
    NSDictionary *dict = st.lx_dict;
    NSLog(@"复杂模型转字典:%@", dict);
}

//5.字典数组转模型数组
- (void)dictArrayToModelArray
{
    NSArray *arr = @[
        @{
            @"english": @(90),
            @"math": @(87),
            @"chinese": @(59)
        },
        @{
            @"english": @(55),
            @"math": @(67),
            @"chinese": @(76)
            
        },
        @{@"english":
            @(78),
            @"math": @(82),
            @"chinese": @(59)
          
          }
    ];
    NSArray *progressArr = [LXProgress lx_objectsArrayWithDictArray:arr];
    
    for (LXProgress *progress in progressArr) {
        NSDictionary *dict = progress.lx_dict;
        NSLog(@"字典数组转模型数组:%@", dict);
    }
}

//6.字典转模型,字典和模型属性值不同，key值映射
- (void)dictToModelWithMap
{
    NSDictionary *dict = @{
        @"name1": @"aaa",
        @"age": @(23),
        @"age11": @(129999),
        @"progress1":@{
            @"math": @(99),
            @"chinese": @(78),
            @"english": @(20)
        }
    };
    
    NSDictionary *map = @{
                          @"name": @"name1",
                          @"progress": @"progress1"
                          };
    
    LXStudent *st = [LXStudent lx_objectWithMap:dict map:map];
    NSLog(@"字典转模型,字典和模型属性值不同，key值映射:name:%@---age:%d", st.name, st.age);
    
    LXProgress *progress = st.progress;
    NSLog(@"字典转模型,字典和模型属性值不同，key值映射:math:%d---chinese:%d---english:%d", progress.math, progress.chinese, progress.english);
}


@end
